#include "..\\include\Lib_Clara.h"
#include "..\\include\Dir.h"
#include "..\\include\cfg_items.h"
#include "conf_loader.h"
#include "config_data.h"

BOOK * SCBook;

typedef struct
{
  BOOK * book;
}MSG;

int NewKey(int key, int r1 , int mode)
{
  BOOK * bk = DISPLAY_GetTopBook(0);
  BOOK * Audio = FindBook(isAudioPlayerBook());
  if(isKeylocked() == 1 && IfLocked==1 && Audio)
  {
    if(key==(KEY1) && mode==(KEYMODE1))
     {
       UI_Event(UI_MEDIAPLAYER_PREV_TRACK_EVENT);
      }
     else if (key==(KEY2) && mode==(KEYMODE2))
     {
      UI_Event(UI_MEDIAPLAYER_NEXT_TRACK_EVENT);
     }
     else if (key==(KEY3) && mode==(KEYMODE3))
     {
       UI_Event(VOLUMEUPKEY_SHORT_PRESS_EVENT);
     }
     else if (key==(KEY4) && mode==(KEYMODE4))
     {
       UI_Event(VOLUMEDOWNKEY_SHORT_PRESS_EVENT);
     }
     else if(PPEnabled==1 && key==(KEY5) && mode==(KEYMODE5))
     {
       UI_Event(KEYPLAYSTOP_PRESSED_EVENT);
     }
  }
  return (0);
}

DISP_OBJ_ONKEY_METHOD OldOnKey;

void NewOnKey(DISP_OBJ *p, int key, int i2, int i3, int mode)
{
  BOOK * Audio = FindBook(isAudioPlayerBook());
  if (MainInput_getVisible(SBY_GetMainInput(Find_StandbyBook()))==0 && Audio==0)
   {
     if(PPEnabled==1 && key==(KEY5) && mode==(KEYMODE5))
     {
       UI_Event(KEYPLAYSTOP_PRESSED_EVENT);
     }
    else
    {
      OldOnKey(p,key,i2,i3,mode);
     }
    }  
   else if (MainInput_getVisible(SBY_GetMainInput(Find_StandbyBook()))==0 && Audio)
   {
     if(key==(KEY1) && mode==(KEYMODE1))
     {
       UI_Event(UI_MEDIAPLAYER_PREV_TRACK_EVENT);
      }
     else if (key==(KEY2) && mode==(KEYMODE2))
     {
      UI_Event(UI_MEDIAPLAYER_NEXT_TRACK_EVENT);
     }
     else if (key==(KEY3) && mode==(KEYMODE3))
     {
       UI_Event(VOLUMEUPKEY_SHORT_PRESS_EVENT);
     }
     else if (key==(KEY4) && mode==(KEYMODE4))
     {
       UI_Event(VOLUMEDOWNKEY_SHORT_PRESS_EVENT);
     }
     else if(PPEnabled==1 && key==(KEY5) && mode==(KEYMODE5))
     {
       UI_Event(KEYPLAYSTOP_PRESSED_EVENT);
     }
    else 
    {
      OldOnKey(p,key,i2,i3,mode);
     }
    }
   else
   {
     OldOnKey(p,key,i2,i3,mode);
   }
}

DISP_OBJ * MI_GetDO(void)
{
  BOOK * b = Find_StandbyBook();
  GUI * g = SBY_GetMainInput(b);
  DISP_OBJ * d = GUIObj_GetDISPObj(g);
  return(d);
}

void MI_SetNewOnKey(void)
{
  DISP_OBJ * d = MI_GetDO();
  OldOnKey = DISP_OBJ_GetOnKey(d);
  DISP_DESC_SetOnKey (DISP_OBJ_GetDESC(d) , (DISP_OBJ_ONKEY_METHOD)NewOnKey);
}

void MI_SetOldOnKey(void)
{
  DISP_OBJ * d = MI_GetDO();
  DISP_DESC_SetOnKey (DISP_OBJ_GetDESC(d) , OldOnKey);
}

int TerminateElf(void * ,BOOK* book)
{
  MI_SetOldOnKey();
  ModifyKeyHook(NewKey,0);
  FreeBook(book);
  return(1);
}

int ShowAuthorInfo(void *mess ,BOOK* book)
{
  MSG * msg = (MSG*)mess;
  MessageBox(0x6fFFFFFF,STR("Song Changer v1.3\n(c)2009 mc_kibel\n\nBig thanks to Slawwan for help with code and Jamesbond22 for events!"),0, 1 ,10000,msg->book);
  return(1);
}

static int ReconfigElf(void *mess ,BOOK *book)
{
  RECONFIG_EVENT_DATA *reconf=(RECONFIG_EVENT_DATA *)mess;
  int result=0;
  if (wstrcmpi(reconf->path,successed_config_path)==0 && wstrcmpi(reconf->name,successed_config_name)==0)
  {
    InitConfig();
    result=1;
  }
  return(result);
}

const PAGE_MSG SC_PageEvents[]@ "DYN_PAGE" ={
  ELF_TERMINATE_EVENT , TerminateElf,
  ELF_SHOW_INFO_EVENT  , ShowAuthorInfo,
  ELF_RECONFIG_EVENT,    ReconfigElf,
  NIL_EVENT_TAG,           NULL
};

PAGE_DESC base_page ={"SC_BasePage",0,SC_PageEvents};


void elf_exit(void)

{
  MI_SetOldOnKey();
  kill_data(&ELF_BEGIN, (void(*)(void*))mfree_adr());
}


void onCloseSCBook(BOOK * book)
{
  if (book)
  {
    SUBPROC(elf_exit);
  }
}

BOOK * CreateSCBook()
{
  SCBook= new BOOK;
  CreateBook(SCBook,onCloseSCBook,&base_page,"Song Changer v1.3",-1,0);
  return(SCBook);
}

int main (void)
{
  InitConfig();
  MI_SetNewOnKey();
  ModifyKeyHook(NewKey,1);
  CreateSCBook();
  return(0);
}
